package com.lc.guessTheWords.layer;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.lc.guessTheWords.MainActivity;
import com.lc.guessTheWords.R;
import com.lc.guessTheWords.core.GameSystem;
import com.lc.guessTheWords.core.StageManager;
import com.lc.guessTheWords.entity.CharacterLocationInfo;
import com.lc.guessTheWords.entity.MapInfo;
import com.lc.guessTheWords.entity.MatrixCharInfo;
import com.lc.guessTheWords.entity.StageData;
import com.lc.guessTheWords.entity.WordInfo;
import com.lc.guessTheWords.particle.ParticleFlower;
import com.lc.guessTheWords.scene.GameScene;
import com.lc.guessTheWords.sprite.CharacterBlock;
import com.lc.guessTheWords.sprite.TipBlock;
import com.lc.guessTheWords.sprite.VisualKeyBoard;
import com.lc.guessTheWords.util.WordMapLoader;
import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.particle.ParticleSystem;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;

/**
 * 游戏层 ，目前所有的游戏逻辑都在该层中，需要进行提取
 * 
 * @author davelin
 * 
 */
public class GameLayer extends Layer {

	public static GameLayer instance;

	public GameLayer() {
		instance = this;
		senseBlocks_horizon = new ArrayList<CharacterBlock>();
		senseBlocks_vertical = new ArrayList<CharacterBlock>();
		blocks = new CharacterBlock[GameSystem.GameRows][GameSystem.GameColumns];
	}

	CharacterBlock[][] blocks;
	CharacterBlock selectedBlock;
	List<CharacterBlock> senseBlocks_horizon;
	List<CharacterBlock> senseBlocks_vertical;
	int currentRow = -1;
	int currentColumn = -1;

	TipBlock tip;
	VisualKeyBoard keyboard;
	Sprite background;
	Sprite charsBack;

	Timer gameTimer;

	// 暂停功能
	Button btnPause;
	Sprite pauseBackground;
	Button btnContinue;
	Button btnReturnStage;
	Button btnMainMenu;
	Button btnClearStage;
	Label labelCurrentStage;
	Label labelCompleteInfo;
	Label labelTime;
	Label labelScore;
	boolean isPause;
	Sprite bottomBack;

	public synchronized void startGame(int index) {
		// ADHelper.showAd();
		isPause = false;

		// 当前关卡
		StageManager.instance.setLastStage(index);

		initCharMatrix();
		setCharMatrix(index);
		initKeyboard();
		initTip();
		initTimer(index);
		initOtherUI();
		selectedBlock = null;
	}

	public synchronized void endGame() {
		// ADHelper.hideAd();

		if (gameTimer != null) {
			gameTimer.cancel();
			gameTimer.purge();
		}
	}

	private void initTimer(int index) {
		if (gameTimer != null) {
			Log.d("lc timer", "回收timer");
			gameTimer.cancel();
			gameTimer.purge();
		}

		gameTimer = new Timer();
		gameTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				// Log.d("lc timer", "di da...");
				if (!isPause) {
					StageData sd = StageManager.instance.getCurrentStageData();
					sd.setTotalSeconds(sd.getTotalSeconds() + 1);
				}
			}
		}, 1000, 1000);
	}

	private void initCharMatrix() {

		senseBlocks_horizon.clear();
		senseBlocks_vertical.clear();

		// this.removeAllChildren(true);
		// if (tip != null) {
		// tip.autoRelease(true);
		// }
		// if (keyboard != null) {
		// keyboard.autoRelease();
		// }

		// 背景
		if (background == null) {
			background = Sprite.make(GameSystem.tex_game_background);
			background.setAnchor(0, 0);
			background.setPosition(WYPoint.make(0, 0));
			background.setScaleX(GameSystem.Screen_Width
					/ GameSystem.tex_game_background.getWidth());
			background.setScaleY(GameSystem.Screen_Height
					/ GameSystem.tex_game_background.getHeight());
			this.addChild(background);
		}

		// 填字背景
		if (charsBack == null) {
			charsBack = Sprite.make(GameSystem.tex_chars_back);
			charsBack.setPosition(WYPoint.make(240, GameSystem.GameMatrixStartY
					- 5 * GameSystem.CharSize));
			this.addChild(charsBack);
		}

		currentRow = -1;
		currentColumn = -1;

		// 创建对象矩阵
		for (int i = 0; i < GameSystem.GameRows; i++) {
			for (int j = 0; j < GameSystem.GameColumns; j++) {
				if (blocks[i][j] == null) {
					CharacterBlock block = new CharacterBlock();
					block.setPosition(GameSystem.Margin_Side + j
							* GameSystem.CharSize + GameSystem.CharSize / 2.0f,
							GameSystem.GameMatrixStartY - i
									* GameSystem.CharSize - GameSystem.CharSize
									/ 2.0f);
					// 初始化均为不可用状态
					block.changeBackgroundType(CharacterBlock.BlockType.NoChar);
					block.setRow(i);
					block.setColumn(j);
					blocks[i][j] = block;
					this.addChild(block);
				} else {
					blocks[i][j].clearBlock();
				}
			}
		}

	}

	/**
	 * 根据数据源设置矩阵状态
	 */
	private void setCharMatrix(int index) {
		// 读取矩阵数据，并设置矩阵状态
		// 读取关卡信息
		MapInfo mapInfo = null;
		if (StageManager.instance.mapInfos.containsKey(index)) {
			mapInfo = StageManager.instance.mapInfos.get(index);
		} else {
			mapInfo = WordMapLoader.loadMap(index);
			StageManager.instance.mapInfos.put(index, mapInfo);
		}
		StageData stageData = StageManager.instance.getStageDatas().get(index);
		for (CharacterLocationInfo location : mapInfo.getCharLocationList()) {

			CharacterBlock aimBlock = blocks[location.getRow()][location
					.getColumn()];
			aimBlock.changeBackgroundType(CharacterBlock.BlockType.Normal);
			if (location.getOrientationWordIndex() >= 0) {
				// 横向有数据
				WordInfo word = mapInfo.getWordList().get(
						location.getOrientationWordIndex());
				aimBlock.setAimHorizon(
						String.valueOf(word.getPinyin().charAt(
								location.getOrientationCharIndex())),
						String.valueOf(word.getContent().charAt(
								location.getOrientationCharIndex())));
				// 设置Tip
				aimBlock.setHorizontalTip(word.getTip());
				aimBlock.setHorizonWordIndex(location.getOrientationWordIndex());
			}
			if (location.getVerticalWordIndex() >= 0) {

				// 竖向
				WordInfo word = mapInfo.getWordList().get(
						location.getVerticalWordIndex());
				aimBlock.setAimVertical(
						String.valueOf(word.getPinyin().charAt(
								location.getVerticalCharIndex())),
						String.valueOf(word.getContent().charAt(
								location.getVerticalCharIndex())));
				// 设置Tip
				aimBlock.setVerticalTip(word.getTip());
				aimBlock.setVerticalWordIndex(location
						.getOrientationWordIndex());
			}
			// 根据历史完成情况进行设置
			if (stageData != null) {
				int blockKey = location.getRow() * GameSystem.GameColumns
						+ location.getColumn();
				if (stageData.getCharInfoList().containsKey(blockKey)) {
					MatrixCharInfo info = stageData.getCharInfoList().get(
							blockKey);
					if (info.isComplete()) {
						aimBlock.setComplete();
					} else {
						aimBlock.setShowChar(info.getCharStr());
					}
				}
			}
		}
	}

	/**
	 * 初始化键盘区域
	 */
	private void initKeyboard() {
		if (keyboard == null) {
			keyboard = new VisualKeyBoard();
			if (StageManager.instance.isBuyNoAD()) {
				keyboard.setPosition(0, 0);
			} else {
				keyboard.setPosition(0, 75);
			}
			this.addChild(keyboard);
		}
	}

	/**
	 * 初始化其他UI元素
	 */
	private void initOtherUI() {
		if (btnPause == null) {
			// 暂停按钮
			btnPause = Button.make(R.drawable.pause_button_normal,
					R.drawable.pause_button_pressed, this, "onPauseClicked");
			btnPause.setAnchor(1, 1);
			btnPause.setPosition(480, 800);
			btnPause.setZOrder(99);
			btnPause.setScale(0.8f);
			this.addChild(btnPause);
			btnPause.setVisible(false);

			// 暂停背景
			pauseBackground = Sprite.make(GameSystem.tex_pause_back);
			pauseBackground.setAnchor(0, 0);
			pauseBackground.setPosition(0, 0);
			pauseBackground.setVisible(false);
			pauseBackground.setZOrder(101);
			this.addChild(pauseBackground);

			// 继续按钮
			btnContinue = Button.make(R.drawable.continue_button_normal,
					R.drawable.continue_button_pressed, this,
					"onContinueClicked");
			btnContinue.setAnchor(0.5f, 0.5f);
			btnContinue.setPosition(240, 570);
			btnContinue.setVisible(false);
			btnContinue.setZOrder(101);
			this.addChild(btnContinue);

			// 返回关卡选择按钮
			btnReturnStage = Button.make(R.drawable.returnstage_button_normal,
					R.drawable.returnstage_button_pressed, this,
					"onStageClicked");
			btnReturnStage.setAnchor(0.5f, 0.5f);
			btnReturnStage.setPosition(240, 450);
			btnReturnStage.setVisible(false);
			btnReturnStage.setZOrder(101);
			this.addChild(btnReturnStage);

			// 返回主菜单按钮
			btnMainMenu = Button
					.make(R.drawable.return_button_normal,
							R.drawable.return_button_pressed, this,
							"onMainMenuClicked");
			btnMainMenu.setAnchor(0.5f, 0.5f);
			btnMainMenu.setPosition(240, 330);
			btnMainMenu.setVisible(false);
			// this.addChild(btnMainMenu);

			// 清空关卡内容
			btnClearStage = Button.make(R.drawable.clear_button_normal,
					R.drawable.clear_button_pressed, this,
					"onClearStageClicked");
			btnClearStage.setAnchor(0.5f, 0.5f);
			btnClearStage.setPosition(240, 330);
			btnClearStage.setVisible(false);
			btnClearStage.setZOrder(101);
			this.addChild(btnClearStage);

			labelCurrentStage = Label.make("关卡-1", 50, Typeface.BOLD);
			labelCurrentStage.setColor(WYColor3B.make(255, 255, 255));
			labelCurrentStage.setAnchor(0.5f, 0.5f);
			labelCurrentStage.setPosition(240, 687);
			labelCurrentStage.setVisible(false);
			labelCurrentStage.setZOrder(101);
			this.addChild(labelCurrentStage);

			labelCompleteInfo = Label.make("完成情况:20/24", 35, Typeface.BOLD);
			labelCompleteInfo.setColor(WYColor3B.make(255, 255, 255));
			labelCompleteInfo.setAnchor(0.5f, 0.5f);
			labelCompleteInfo.setPosition(240, 230);
			labelCompleteInfo.setVisible(false);
			labelCompleteInfo.setZOrder(101);
			this.addChild(labelCompleteInfo);

			labelTime = Label.make("累计用时:2'30", 35, Typeface.BOLD);
			labelTime.setColor(WYColor3B.make(255, 255, 255));
			labelTime.setAnchor(0.5f, 0.5f);
			labelTime.setPosition(240, 180);
			labelTime.setVisible(false);
			labelTime.setZOrder(101);
			this.addChild(labelTime);

			labelScore = Label.make("得分:20000", 35, Typeface.BOLD);
			labelScore.setColor(WYColor3B.make(255, 255, 255));
			labelScore.setAnchor(0.5f, 0.5f);
			labelScore.setPosition(240, 130);
			labelScore.setVisible(false);
			labelScore.setZOrder(101);
			this.addChild(labelScore);

			if (!StageManager.instance.isBuyNoAD()) {
				bottomBack = Sprite.make(GameSystem.tex_gamebuttom_back);
				bottomBack.setAnchor(0, 0);
				bottomBack.setPosition(0, 0);
				this.addChild(bottomBack);
			}
		}
		btnPause.setVisible(false);
		labelScore.setVisible(false);
		labelTime.setVisible(false);
		labelCompleteInfo.setVisible(false);
		labelCurrentStage.setVisible(false);
		btnClearStage.setVisible(false);
		btnReturnStage.setVisible(false);
		btnContinue.setVisible(false);
		pauseBackground.setVisible(false);
		btnPause.setVisible(false);
	}

	public void onContinueClicked() {
		isPause = false;
		pauseBackground.setVisible(false);
		btnClearStage.setVisible(false);
		btnContinue.setVisible(false);
		btnMainMenu.setVisible(false);
		btnReturnStage.setVisible(false);

		// btnPause.setVisible(true);

		labelCurrentStage.setVisible(false);
		labelCompleteInfo.setVisible(false);
		labelTime.setVisible(false);
		labelScore.setVisible(false);
	}

	public void onStageClicked() {
		GameScene.make().returnToStageScene();
	}

	public void onMainMenuClicked() {
		// 暂时没用
	}

	public void onClearStageClicked() {
		// 清空本关卡
		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				AlertDialog alertDialog = new AlertDialog.Builder(
						MainActivity.instance)
						.setTitle("提示")
						.setMessage("确定重玩本关卡吗？")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										clearStage();

									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										return;
									}
								}).setCancelable(false).create(); // 创建对话框
				alertDialog.show();
			}
		});
	}

	public void clearStage() {
		StageManager.instance.clearCurrentStage();
		this.startGame(StageManager.instance.getLastStage());
	}

	public void onPauseClicked() {
		pauseGame();
	}

	public void pauseGame() {
		// 暂停按钮点击
		isPause = true;
		pauseBackground.setVisible(true);
		btnClearStage.setVisible(true);
		btnContinue.setVisible(true);
		btnMainMenu.setVisible(true);
		btnReturnStage.setVisible(true);

		btnPause.setVisible(false);

		// 读取信息
		int stageCount = 24;
		if (StageManager.instance.mapWordCounts
				.containsKey(StageManager.instance.getLastStage())) {
			stageCount = StageManager.instance.mapWordCounts
					.get(StageManager.instance.getLastStage());
		}
		StageData sd = StageManager.instance.getCurrentStageData();
		if (sd != null) {
			labelCurrentStage.setText("关卡-"
					+ StageManager.instance.getLastStage());
			labelCompleteInfo.setText("完成情况:" + sd.getComplateCount() + "/"
					+ stageCount);
			labelScore.setText("得分:" + sd.getScore());
			labelTime.setText("累计用时:" + getUseTimeStr(sd.getTotalSeconds()));
		}

		labelCurrentStage.setVisible(true);
		labelCompleteInfo.setVisible(true);
		labelTime.setVisible(true);
		labelScore.setVisible(true);
	}

	static DecimalFormat secondFormat = new DecimalFormat("00");

	private String getUseTimeStr(long totalSeconds) {
		return (totalSeconds / 60) + "'"
				+ secondFormat.format(totalSeconds % 60);
	}

	private void initTip() {
		if (tip == null) {
			tip = new TipBlock();
			if (StageManager.instance.isBuyNoAD()) {
				tip.setPosition(240, 700f);
			} else {
				tip.setPosition(240, 737.5f);
			}
			tip.setZOrder(100);
			this.addChild(tip);
		}
		tip.setDefault();
	}

	// 处理触摸事件
	@Override
	public boolean wyTouchesBegan(MotionEvent event) {
		if (!isPause) {
			// 转换坐标系
			WYPoint point = this.convertTouchToNodeSpace(event);

			// process
			int column = getBlockIndex(point.x, GameSystem.Margin_Side,
					GameSystem.CharSize, true);
			int row = getBlockIndex(point.y, GameSystem.GameMatrixStartY,
					GameSystem.CharSize, false);

			changeSelectBlock(column, row);
		}

		return true;
	}

	/**
	 * 切换选择的block
	 * 
	 * @param column
	 * @param row
	 */
	private void changeSelectBlock(int column, int row) {
		if (row >= 0 && row < GameSystem.GameRows && column >= 0
				&& column < GameSystem.GameColumns) {

			if (blocks[row][column].getCurrentType() != CharacterBlock.BlockType.NoChar) {
				if (selectedBlock != null) {
					selectedBlock.changeBackgroundType(0);
				}

				blocks[row][column].changeBackgroundType(2);
				selectedBlock = blocks[row][column];

				currentRow = row;
				currentColumn = column;

				findSenseBlocks(row, column);

				// show tip
				tip.setTip(selectedBlock.getHorizontalTip(),
						selectedBlock.getVerticalTip());
			} else {
				clearSelection();
			}
			isPreviousRight = true;
		}
	}

	/**
	 * 清除选中状态
	 */
	private void clearSelection() {
		// 选中了nochar block, 清楚select以及sense,恢复提示
		if (selectedBlock != null) {
			selectedBlock.changeBackgroundType(0);
			selectedBlock = null;
		}
		for (CharacterBlock block : senseBlocks_horizon) {
			block.changeBackgroundType(CharacterBlock.BlockType.Normal);
		}
		for (CharacterBlock block : senseBlocks_vertical) {
			block.changeBackgroundType(CharacterBlock.BlockType.Normal);
		}
		tip.setDefault();
	}

	/**
	 * 找到感应显示块，并进行提示
	 * 
	 * @param row
	 * @param column
	 */
	private void findSenseBlocks(int row, int column) {

		// 恢复状态
		for (CharacterBlock block : senseBlocks_horizon) {
			if (block.getCurrentType() != CharacterBlock.BlockType.Selected) {
				block.changeBackgroundType(CharacterBlock.BlockType.Normal);
			}
		}

		for (CharacterBlock block : senseBlocks_vertical) {
			if (block.getCurrentType() != CharacterBlock.BlockType.Selected) {
				block.changeBackgroundType(CharacterBlock.BlockType.Normal);
			}
		}

		// 清空
		senseBlocks_horizon.clear();
		senseBlocks_vertical.clear();

		// 横向向左
		if (column > 0) {
			for (int i = column - 1; i >= 0; i--) {
				if (blocks[row][i].getCurrentType() == CharacterBlock.BlockType.NoChar) {
					break;
				}
				senseBlocks_horizon.add(blocks[row][i]);
			}
		}
		// 横向向右
		if (column < GameSystem.GameColumns - 1) {
			for (int i = column + 1; i < GameSystem.GameColumns; i++) {
				if (blocks[row][i].getCurrentType() == CharacterBlock.BlockType.NoChar) {
					break;
				}
				senseBlocks_horizon.add(blocks[row][i]);
			}
		}
		// 竖向向上
		if (row > 0) {
			for (int i = row - 1; i >= 0; i--) {
				if (blocks[i][column].getCurrentType() == CharacterBlock.BlockType.NoChar) {
					break;
				}
				senseBlocks_vertical.add(blocks[i][column]);
			}
		}
		// 竖向向下
		if (row < GameSystem.GameRows - 1) {
			for (int i = row + 1; i < GameSystem.GameRows; i++) {
				if (blocks[i][column].getCurrentType() == CharacterBlock.BlockType.NoChar) {
					break;
				}
				senseBlocks_vertical.add(blocks[i][column]);
			}
		}

		// 切换显示状态
		for (CharacterBlock block : senseBlocks_horizon) {
			block.changeBackgroundType(CharacterBlock.BlockType.SenseHorizon);
		}
		for (CharacterBlock block : senseBlocks_vertical) {
			block.changeBackgroundType(CharacterBlock.BlockType.SenseVertical);
		}
	}

	/**
	 * 按下按钮之后的处理
	 * 
	 * @param keyStr
	 */
	public synchronized void touchKey(String keyStr) {
		if (!isPause && selectedBlock != null) {
			if (!selectedBlock.isCompleted()) {

				// 首先判断是否是lamp
				if (keyStr.equalsIgnoreCase("lamp")) {

					if (StageManager.instance.getLampCount() > 0) {
						selectedBlock.setComplete();
						// 减少灯泡数量
						StageManager.instance
								.setLampCount(StageManager.instance
										.getLampCount() - 1);
						keyboard.updateLampCount();
					} else {
						// 没有可用的灯泡了，提示用户可以点击广告或者进入积分墙获取灯泡
						MainActivity.instance.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								Toast.makeText(MainActivity.instance,
										"没有提示可用了，可以通过道具商店免费获取提示道具",
										Toast.LENGTH_SHORT).show();
							}
						});
					}
				} else {
					selectedBlock.setShowChar(keyStr);
				}

				// 判断词条是否完全匹配，显示完整词条数据
				processJudgeComplete(senseBlocks_horizon, true);
				processJudgeComplete(senseBlocks_vertical, false);
			}
			moveToNextSelectedBlock();
		}
	}

	boolean isPreviousRight = true;

	private void moveToNextSelectedBlock() {
		// 根据上一次的移动动作，进行向下或者向右
		try {
			if (isPreviousRight) {
				// 将选择移动至下个block，先向右，再向下
				if (currentColumn < GameSystem.GameColumns - 1
						&& currentColumn >= 0
						&& currentRow >= 0
						&& currentRow < GameSystem.GameRows
						&& blocks[currentRow][currentColumn + 1]
								.getCurrentType() != CharacterBlock.BlockType.NoChar) {
					changeSelectBlock(currentColumn + 1, currentRow);
					isPreviousRight = true;
				} else if (currentRow < GameSystem.GameRows - 1
						&& currentRow >= 0
						&& currentColumn >= 0
						&& currentColumn < GameSystem.GameColumns
						&& blocks[currentRow + 1][currentColumn]
								.getCurrentType() != CharacterBlock.BlockType.NoChar) {
					changeSelectBlock(currentColumn, currentRow + 1);
					isPreviousRight = false;
				} else {
					clearSelection();
				}
			} else {
				// 将选择移动至下个block，先向下，再向右
				if (currentRow < GameSystem.GameRows - 1
						&& currentRow >= 0
						&& currentColumn >= 0
						&& currentColumn < GameSystem.GameColumns
						&& blocks[currentRow + 1][currentColumn]
								.getCurrentType() != CharacterBlock.BlockType.NoChar) {
					changeSelectBlock(currentColumn, currentRow + 1);
					isPreviousRight = false;
				} else if (currentColumn < GameSystem.GameColumns - 1
						&& currentColumn >= 0
						&& currentRow >= 0
						&& currentRow < GameSystem.GameRows
						&& blocks[currentRow][currentColumn + 1]
								.getCurrentType() != CharacterBlock.BlockType.NoChar) {
					changeSelectBlock(currentColumn + 1, currentRow);
					isPreviousRight = true;
				} else {
					clearSelection();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void processJudgeComplete(List<CharacterBlock> senseList,
			boolean isHorizon) {
		if (senseList.size() > 0) {
			boolean isSuccess = true;
			for (CharacterBlock block : senseList) {
				if (!block.isCorrectPy()) {
					isSuccess = false;
					break;
				}
			}

			// 判断当前点
			if (isSuccess && selectedBlock.isCorrectPy()) {
				// 成功
				selectedBlock.setComplete();
				for (CharacterBlock block : senseList) {
					block.setComplete();
				}
				StageData sd = StageManager.instance.getCurrentStageData();
				sd.setComplateCount(sd.getComplateCount() + 1);

				// 得分
				addScore(sd);

				// 粒子效果Here
				showCharPaticle(selectedBlock, senseList, isHorizon);
			}
		}
	}

	/**
	 * 显示完成词条的粒子效果
	 * 
	 * @param selectedBlock2
	 * @param senseList
	 * @param isHorizon
	 */
	private void showCharPaticle(CharacterBlock selectedBlock,
			List<CharacterBlock> senseList, boolean isHorizon) {
		int count = senseList.size() + 1;
		int sX = -1, eX = -1, sY = -1, eY = -1;
		sX = selectedBlock.getColumn();
		eX = selectedBlock.getColumn();
		sY = selectedBlock.getRow();
		eY = selectedBlock.getRow();
		for (CharacterBlock item : senseList) {
			if (item.getRow() > eY) {
				eY = item.getRow();
			}
			if (item.getRow() < sY) {
				sY = item.getRow();
			}
			if (item.getColumn() > eX) {
				eX = item.getColumn();
			}
			if (item.getColumn() < sX) {
				sX = item.getColumn();
			}
		}
		final ParticleSystem wordShowerParticle = ParticleFlower.make();
		wordShowerParticle.setVisible(false);
		this.addChild(wordShowerParticle);
		MoveTo move = (MoveTo) MoveTo.make(
				count * 0.2f,
				GameSystem.Margin_Side + GameSystem.CharSize / 2f + sX
						* GameSystem.CharSize,
				GameSystem.GameMatrixStartY - GameSystem.CharSize / 2f - sY
						* GameSystem.CharSize,
				GameSystem.Margin_Side + GameSystem.CharSize / 2f + eX
						* GameSystem.CharSize
						+ (isHorizon ? GameSystem.CharSize / 2f : 0),
				GameSystem.GameMatrixStartY - GameSystem.CharSize / 2f - eY
						* GameSystem.CharSize
						- (isHorizon ? 0 : GameSystem.CharSize / 2f))
				.autoRelease();
		move.setCallback(new Callback() {

			@Override
			public void onUpdate(int arg0, float arg1) {

			}

			@Override
			public void onStop(int arg0) {
				wordShowerParticle.setVisible(false);
				wordShowerParticle.autoRelease();
				GameLayer.this.removeChild(wordShowerParticle, true);
			}

			@Override
			public void onStart(int arg0) {
				wordShowerParticle.setVisible(true);
			}
		});
		wordShowerParticle.runAction(move);
	}

	/**
	 * 得分Here
	 * 
	 * @param sd
	 */
	private void addScore(StageData sd) {
		if (sd.getTotalSeconds() <= 200) {
			sd.setScore(sd.getScore() + 800);
		} else if (sd.getTotalSeconds() <= 400) {
			sd.setScore(sd.getScore() + 500);
		} else if (sd.getTotalSeconds() <= 600) {
			sd.setScore(sd.getScore() + 200);
		} else {
			sd.setScore(sd.getScore() + 100);
		}
	}

	/**
	 * 获取block的index
	 * 
	 * @param point
	 *            当前坐标
	 * @param startPoint
	 *            起始位置
	 * @param blockSize
	 *            块像素大小
	 * @param isAesc
	 *            是否是从小到大
	 * @return
	 */
	private int getBlockIndex(float point, int startPoint, float blockSize,
			boolean isAesc) {
		int result = -1;
		if (isAesc) {
			if (point >= startPoint) {
				result = (int) ((point - startPoint) / blockSize);
			}
		} else {
			if (startPoint > point) {
				result = (int) ((startPoint - point) / blockSize);
			}
		}
		return result;
	}
}
